/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.eventos.adapaters;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author lizardo
 */
public class SrvImpresiones extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private String consejo;
    private String abrev;
    private String titulo;
    private String partiipante;
    private String tipoEvento;
    private String evento;
    private String fecha;
    private String decano;
    private String instructor;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/vnd.ms-excel");

        //carga de datos
        cargaDatos();

        HttpServletRequestWrapper srw = new HttpServletRequestWrapper(request);
        String ruta = srw.getRealPath("/excel/impresiones.xls");
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(ruta));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);

        HSSFRow row2 = sheet.getRow(1);     //
        HSSFCell a2 = row2.getCell(0);      //Consejo Departamental
        a2.setCellValue(consejo);

        HSSFRow row3 = sheet.getRow(2);     //
        HSSFCell a3 = row3.getCell(0);      //Abreviatura Consejo Deptal
        a3.setCellValue(abrev);

        HSSFRow row8 = sheet.getRow(7);     //
        HSSFCell d8 = row8.getCell(3);      //Titulo certificado
        d8.setCellValue(titulo);

        HSSFRow row9 = sheet.getRow(8);     //
        HSSFCell d9 = row9.getCell(3);      //Nombre del participante
        d9.setCellValue(partiipante);
        
        HSSFRow row10 = sheet.getRow(9);    //
        HSSFCell c10 = row10.getCell(2);    //Tipo de evento
        c10.setCellValue(tipoEvento);
        HSSFCell e10 = row10.getCell(4);    //Nombre del evento
        e10.setCellValue(evento);

        HSSFRow row17 = sheet.getRow(16);   //
        HSSFCell b17 = row17.getCell(2);    //Fecha
        b17.setCellValue(fecha);
        
        HSSFRow row23 = sheet.getRow(22);   //
        HSSFCell b23 = row23.getCell(1);    //Nombre del Decano
        b23.setCellValue(decano);
        HSSFCell e23 = row23.getCell(4);    //Nombre del Instructor
        e23.setCellValue(instructor);

        HSSFRow row24 = sheet.getRow(23);   //
        HSSFCell b24 = row24.getCell(1);    //Abreviatura Consejo Deptal
        b24.setCellValue(abrev);

        //String nota11 = "0";
        //b2.setCellValue(Integer.valueOf(nota11).intValue());

        sheet.setForceFormulaRecalculation(true);   // actualiza sheet

        OutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();


    }

    private void cargaDatos() {
        consejo = "Consejo Departamental La Libertad";
        abrev = "CDLL";
        titulo = "Participación";
        partiipante = "Lizardo Felipe Rios Villavicencio";
        tipoEvento = "Diplomado";
        evento = "Gestión de Riesgos";
        fecha = "07 de Septiembre de 2009";
        decano = "Ing. Fernando Rodríguez";
        instructor = "Michael Gutierrez";
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
